package com.fr.cell.editor;

import com.fr.base.FRFont;
import com.fr.base.Style;
import com.fr.base.background.ColorBackground;
import com.fr.cell.Grid;
import com.fr.cell.core.EditTextField;
import com.fr.report.FloatElement;
import com.fr.util.Utils;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;

public class TextFloatEditor extends AbstractFloatEditor
{
  private Grid grid;
  private FloatElement floatElement;
  protected EditTextField textField = new EditTextField();
  protected String oldValue = "";
  private KeyListener textKeyListener = new KeyListener(this)
  {
    private final TextFloatEditor this$0;

    public void keyTyped()
    {
      TextFloatEditor.access$000(this.this$0);
    }

    public void keyPressed()
    {
      int i = paramKeyEvent.getKeyCode();
      if (i == 27)
      {
        this.this$0.textField.setText(this.this$0.oldValue);
        paramKeyEvent.consume();
      }
      else if ((i == 10) || (i == 9))
      {
        TextFloatEditor.access$100(this.this$0).requestFocus();
        TextFloatEditor.access$100(this.this$0).dispatchEvent(paramKeyEvent);
      }
    }

    public void keyReleased()
    {
    }
  };

  public Grid getGrid()
  {
    return this.grid;
  }

  public FloatElement getFloatElement()
  {
    return this.floatElement;
  }

  public TextFloatEditor()
  {
    this.textField.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 1));
    this.textField.addKeyListener(this.textKeyListener);
    this.textField.addFocusListener(new FocusAdapter(this)
    {
      private final TextFloatEditor this$0;

      public void focusLost()
      {
        this.this$0.stopFloatEditing();
      }
    });
    this.textField.setFocusTraversalKeysEnabled(false);
  }

  public int getMaxLength()
  {
    return this.textField.getMaxLength();
  }

  public void setMaxLength(int paramInt)
  {
    this.textField.setMaxLength(paramInt);
  }

  public Object getFloatEditorValue()
    throws Exception
  {
    return this.textField.getText();
  }

  public Component getFloatEditorComponent(Grid paramGrid, FloatElement paramFloatElement)
  {
    this.grid = paramGrid;
    this.floatElement = paramFloatElement;
    Object localObject = paramFloatElement.getValue();
    if (localObject == null)
      localObject = "";
    if ((!(localObject instanceof String)) && (!(localObject instanceof Number)))
      localObject = "";
    this.oldValue = Utils.objectToString(localObject);
    this.textField.setText(this.oldValue);
    Style localStyle = paramFloatElement.getStyle();
    ajustTextStyle(paramGrid, localStyle, localObject);
    return ((Component)this.textField);
  }

  protected void ajustTextStyle(Grid paramGrid, Style paramStyle, Object paramObject)
  {
    if (paramStyle == null)
      paramStyle = Style.DEFAULT_STYLE;
    int i = paramStyle.getHorizontalAlignment();
    if (i == 2)
      this.textField.setHorizontalAlignment(2);
    else if (i == 0)
      this.textField.setHorizontalAlignment(0);
    else if (i == 4)
      this.textField.setHorizontalAlignment(4);
    else
      this.textField.setHorizontalAlignment(2);
    FRFont localFRFont = paramStyle.getFRFont();
    this.textField.setFont(new Font(localFRFont.getFontName(), localFRFont.getStyle(), localFRFont.getSize()));
    this.textField.setForeground(paramStyle.getFRFont().getForeground());
    if (paramStyle.getBackground() instanceof ColorBackground)
      this.textField.setBackground(((ColorBackground)paramStyle.getBackground()).getColor());
    else
      this.textField.setBackground(Color.white);
  }

  private void ajustTextFieldSize()
  {
    Dimension localDimension1 = this.textField.getSize();
    Dimension localDimension2 = this.textField.getPreferredSize();
    this.textField.setSize((int)Math.max(localDimension1.getWidth(), localDimension2.getWidth()), (int)localDimension1.getHeight());
  }

  static void access$000(TextFloatEditor paramTextFloatEditor)
  {
    paramTextFloatEditor.ajustTextFieldSize();
  }

  static Grid access$100(TextFloatEditor paramTextFloatEditor)
  {
    return paramTextFloatEditor.grid;
  }
}